package com.github.pig.nideshop.controller.user;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.ZyResult;
import com.github.pig.nideshop.entity.*;
import com.github.pig.nideshop.model.vo.ProductVo;
import com.github.pig.nideshop.service.INideshopCartService;
import com.github.pig.nideshop.service.INideshopOrderGoodsService;
import com.github.pig.nideshop.service.INideshopOrderService;
import com.github.pig.nideshop.service.INideshopProductService;
import org.apache.commons.lang.time.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  订单控制器前端控制器
 * </p>
 *
 * @author J神
 * @since 2018-06-11
 */
@RestController
@RequestMapping("/orders")
public class NideshopOrderController {
    @Autowired
    private INideshopOrderService nideshopOrderService;
    @Autowired
    private INideshopOrderGoodsService nideshopOrderGoodsService;
    @Autowired
    private INideshopCartService iNideshopCartService;
    @Autowired
    private INideshopProductService nideshopProductService;

    /**
     * 我的订单
     * @param order
     * @param page
     * @return
     */
    @GetMapping
    public ZyResult orders(NideshopOrder order, Page page){
        return ZyResult.ok(nideshopOrderService.selectPage(page, new EntityWrapper<>(order)));
    }
    /**
     * 创建订单
     */
    @PostMapping
    public ZyResult order(NideshopOrder nideshopOrder){
        List<NideshopCart> carts=null;
        List<ProductVo> products=null;
        List<Integer> ids=new ArrayList<>();
        List<NideshopOrderGoods> orderGoods=new ArrayList<>();
        Map<Integer,NideshopCart> cartMap=new HashMap<>();
        BigDecimal orderPrice=new BigDecimal(0);
        Map map=new HashMap();
        /**
         * 获取购物车中check的商品
         */
        {
            NideshopCart nideshopCart=new NideshopCart();
            nideshopCart.setUserId(nideshopOrder.getUserId());
            carts=iNideshopCartService.selectList(new EntityWrapper<>(nideshopCart));
            if(carts.size()==0){
                return ZyResult.error(new RuntimeException("购物车无商品"));
            }
        }
        /**
         * 验证库存
         */
        {
            for(NideshopCart cart:carts){
                ids.add(cart.getProductId());
                cartMap.put(cart.getProductId(),cart);
            }
            //获取产品
            products=nideshopProductService.selectProductVos(ids);
            //构造订单详情
            for(ProductVo productVo:products){
                NideshopGoods nideshopGoods=productVo.getNideshopGoods();
                NideshopProduct nideshopProduct=productVo.getNideshopProduct();
                NideshopCart nideshopCart= cartMap.get(productVo.getNideshopProduct().getId());
                NideshopOrderGoods goods=new NideshopOrderGoods();
                goods.setNumber(nideshopCart.getNumber());
                goods.setGoodsName(nideshopGoods.getName());
                goods.setGoodsId(nideshopGoods.getId());
                goods.setGoodsSn(nideshopGoods.getGoodsSn());
                goods.setGoodsSpecifitionIds(nideshopProduct.getGoodsSpecificationIds());
//                goods.setIsReal(nideshopGoods.get);
                goods.setListPicUrl(nideshopGoods.getListPicUrl());
                goods.setMarketPrice(nideshopGoods.getExtraPrice());
//                goods.setOrderId();
                goods.setProductId(nideshopProduct.getId());
                goods.setRetailPrice(nideshopProduct.getRetailPrice());
                goods.setGoodsSpecifitionNameValue("");
                orderPrice.add(nideshopProduct.getRetailPrice().multiply(new BigDecimal(nideshopCart.getNumber())));
                orderGoods.add(goods);
                nideshopOrder.setAddTime(System.currentTimeMillis()/1000);
                nideshopOrder.setOrderPrice(orderPrice);
                nideshopOrder.setOrderSn(org.apache.http.client.utils.DateUtils.formatDate(new Date(),"yyyyMMddHHmmss"));
            }
        }
        /**
         * 保存订单
         */
        {

            nideshopOrderService.insert(nideshopOrder);
            Integer id=nideshopOrder.getId();
            orderGoods.stream().forEach(item->{
                item.setOrderId(id);
                nideshopOrderGoodsService.insert(item);
            });
            map.put("order",nideshopOrder);
            map.put("orderGoods",orderGoods);
        }
        return ZyResult.ok(map);
    }


}

